function channel_coeffs = generate_rayleigh_channel(num_samples, num_taps, sampling_freq, max_delay)
% GENERATE_RAYLEIGH_CHANNEL 生成瑞利衰落信道系数
% 
% 输入参数:
%   num_samples - 采样点数
%   num_taps - 信道抽头数
%   sampling_freq - 采样频率 (Hz)
%   max_delay - 最大时延 (秒)
%
% 输出参数:
%   channel_coeffs - 信道系数矩阵 (num_samples x num_taps)
%
% 示例:
%   coeffs = generate_rayleigh_channel(1000, 6, 1e6, 1e-3);

    % 参数验证
    if nargin < 4
        max_delay = 1e-3; % 默认最大时延 1ms
    end
    if nargin < 3
        sampling_freq = 1e6; % 默认采样频率 1MHz
    end
    if nargin < 2
        num_taps = 6; % 默认抽头数 6
    end
    if nargin < 1
        num_samples = 1000; % 默认采样点数 1000
    end
    
    % 生成时延向量
    delay_resolution = 1/sampling_freq;
    delays = 0:delay_resolution:max_delay;
    
    % 选择抽头时延
    if length(delays) >= num_taps
        tap_delays = round(linspace(1, length(delays), num_taps));
    else
        error('最大时延太小，无法生成指定数量的抽头');
    end
    
    % 生成信道系数
    channel_coeffs = zeros(num_samples, num_taps);
    
    for tap = 1:num_taps
        % 生成独立的瑞利衰落系数
        rayleigh_coeffs = sqrt(0.5) * (randn(num_samples, 1) + 1i * randn(num_samples, 1));
        
        % 应用功率延迟分布 (指数衰减)
        power_factor = exp(-tap_delays(tap) / num_taps);
        channel_coeffs(:, tap) = rayleigh_coeffs * sqrt(power_factor);
    end
    
end

function channel_coeffs = generate_rician_channel(num_samples, k_factor, sampling_method)
% GENERATE_RICIAN_CHANNEL 生成莱斯衰落信道系数
%
% 输入参数:
%   num_samples - 采样点数
%   k_factor - 莱斯因子 (线性值，非dB)
%   sampling_method - 采样方法 ('jakes' 或 'gaussian')
%
% 输出参数:
%   channel_coeffs - 信道系数向量 (num_samples x 1)
%
% 示例:
%   coeffs = generate_rician_channel(1000, 5, 'jakes');

    % 参数验证
    if nargin < 3
        sampling_method = 'gaussian';
    end
    if nargin < 2
        k_factor = 5; % 默认莱斯因子 5 (线性值)
    end
    if nargin < 1
        num_samples = 1000;
    end
    
    % 计算LOS和NLOS分量的功率
    p_los = k_factor / (k_factor + 1); % LOS分量功率
    p_nlos = 1 / (k_factor + 1); % NLOS分量功率
    
    % 生成LOS分量 (常数)
    los_component = sqrt(p_los);
    
    % 生成NLOS分量 (瑞利衰落)
    if strcmp(sampling_method, 'jakes')
        % Jakes模型 - 使用正弦波叠加法生成具有特定多普勒谱的衰落
        % 需要更多参数，这里使用简化的Jakes仿真器
        num_oscillators = 8; % 振荡器数量
        fd = 100; % 假设最大多普勒频移为100Hz
        t = (0:num_samples-1)' / (num_samples-1) * 10; % 时间向量，仿真10秒
        
        % 初始化NLOS分量
        nlos_component = zeros(num_samples, 1);
        
        % 使用多个正弦波叠加生成衰落
        for k = 1:num_oscillators
            alpha_k = 2*pi*k/num_oscillators;
            beta_k = pi*(k-0.5)/num_oscillators;
            
            % 实部和虚部
            nlos_real = cos(2*pi*fd*cos(alpha_k)*t + beta_k);
            nlos_imag = sin(2*pi*fd*cos(alpha_k)*t + beta_k);
            
            nlos_component = nlos_component + (nlos_real + 1i*nlos_imag);
        end
        
        % 归一化功率
        nlos_component = sqrt(p_nlos/2) * nlos_component / sqrt(num_oscillators);
        
    else
        % 高斯随机过程 - 简单的高斯随机变量
        nlos_component = sqrt(p_nlos/2) * (randn(num_samples, 1) + 1i * randn(num_samples, 1));
    end
    
    % 合并LOS和NLOS分量
    channel_coeffs = los_component + nlos_component;
    
end

function path_loss = calculate_path_loss(distance, carrier_freq, path_loss_exponent, reference_distance)
% CALCULATE_PATH_LOSS 计算路径损耗
%
% 输入参数:
%   distance - 传播距离 (米)
%   carrier_freq - 载波频率 (Hz)
%   path_loss_exponent - 路径损耗指数
%   reference_distance - 参考距离 (米)
%
% 输出参数:
%   path_loss - 路径损耗 (dB)
%
% 示例:
%   loss = calculate_path_loss(100, 2.4e9, 3.5, 1);

    % 参数验证
    if nargin < 4
        reference_distance = 1; % 默认参考距离 1米
    end
    if nargin < 3
        path_loss_exponent = 3.5; % 默认路径损耗指数
    end
    if nargin < 2
        carrier_freq = 2.4e9; % 默认载波频率 2.4GHz
    end
    if nargin < 1
        error('必须提供距离参数');
    end
    
    % 计算参考点的自由空间路径损耗
    speed_of_light = 3e8; % 光速
    lambda = speed_of_light / carrier_freq; % 波长
    reference_loss = 20*log10(4*pi*reference_distance/lambda);
    
    % 计算路径损耗
    if distance > reference_distance
        path_loss = reference_loss + 10*path_loss_exponent*log10(distance/reference_distance);
    else
        path_loss = 20*log10(4*pi*distance/lambda); % 自由空间路径损耗
    end
    
end

function shadowing = generate_shadow_fading(num_samples, std_dev, correlation_distance, sampling_distance)
% GENERATE_SHADOW_FADING 生成阴影衰落
%
% 输入参数:
%   num_samples - 采样点数
%   std_dev - 标准差 (dB)
%   correlation_distance - 相关距离 (米)
%   sampling_distance - 采样距离间隔 (米)
%
% 输出参数:
%   shadowing - 阴影衰落值 (dB)
%
% 示例:
%   shadowing = generate_shadow_fading(1000, 8, 50, 1);

    % 参数验证
    if nargin < 4
        sampling_distance = 1; % 默认采样距离 1米
    end
    if nargin < 3
        correlation_distance = 50; % 默认相关距离 50米
    end
    if nargin < 2
        std_dev = 8; % 默认标准差 8dB
    end
    if nargin < 1
        num_samples = 1000;
    end
    
    % 生成高斯随机过程
    gaussian_process = std_dev * randn(num_samples, 1);
    
    % 应用空间相关性
    if correlation_distance > 0 && sampling_distance > 0
        % 计算相关系数
        distances = (0:num_samples-1)' * sampling_distance;
        correlation = exp(-distances / correlation_distance);
        
        % 使用滤波器实现相关性
        filter_length = min(100, num_samples);
        filter_coeffs = exp(-(0:filter_length-1)' * sampling_distance / correlation_distance);
        filter_coeffs = filter_coeffs / sum(filter_coeffs);
        
        % 应用滤波器
        shadowing = conv(gaussian_process, filter_coeffs, 'same');
    else
        shadowing = gaussian_process;
    end
    
end

function doppler_spectrum = generate_doppler_spectrum(fd_max, num_points, spectrum_type)
% GENERATE_DOPPLER_SPECTRUM 生成多普勒谱
%
% 输入参数:
%   fd_max - 最大多普勒频移 (Hz)
%   num_points - 频谱点数
%   spectrum_type - 谱类型 ('jakes', 'gaussian', 'uniform')
%
% 输出参数:
%   doppler_spectrum - 多普勒谱结构体
%
% 示例:
%   spectrum = generate_doppler_spectrum(100, 1000, 'jakes');

    % 参数验证
    if nargin < 3
        spectrum_type = 'jakes';
    end
    if nargin < 2
        num_points = 1000;
    end
    if nargin < 1
        fd_max = 100; % 默认最大多普勒频移 100Hz
    end
    
    % 生成频率向量
    f = linspace(-fd_max, fd_max, num_points);
    
    % 根据谱类型生成多普勒谱
    switch lower(spectrum_type)
        case 'jakes'
            % Jakes谱 (经典多普勒谱)
            S = 1 ./ (pi * fd_max * sqrt(1 - (f/fd_max).^2));
            S(abs(f) >= fd_max) = 0; % 超出范围的频率置零
            S = S / max(S); % 归一化
            
        case 'gaussian'
            % 高斯谱
            sigma = fd_max / 3; % 标准差
            S = exp(-f.^2 / (2*sigma^2));
            S = S / max(S); % 归一化
            
        case 'uniform'
            % 均匀谱
            S = ones(size(f));
            S(abs(f) > fd_max) = 0;
            S = S / max(S); % 归一化
            
        otherwise
            error('不支持的谱类型');
    end
    
    % 返回结构体
    doppler_spectrum.frequencies = f;
    doppler_spectrum.power_spectral_density = S;
    doppler_spectrum.fd_max = fd_max;
    doppler_spectrum.spectrum_type = spectrum_type;
    
end